import {
  getExpire,
  getMenu,
  getToken,
  getUser,
  removeToken,
  setExpire,
  setMenu,
  setToken, setUser
} from '@/utils/storage'
import { getMenuList } from '@/api/dashboard'

/**
 *
 * @type {import('vuex').module}
 */
const user = {
  namespaced: true,
  state: {
    token: getToken(),
    user: getUser(),
    expire: getExpire(),
    menu: getMenu()
  },
  mutations: {
    setToken (state, newToken) {
      setToken(newToken)
      state.token = newToken
    },
    delToken (state) {
      removeToken()
      state.token = null
    },
    setUserInfo (state, newUser) {
      setUser(newUser)
      state.user = newUser
    },
    setTimeOut (state, newTime) {
      setExpire(newTime)
      state.expire = newTime
    },
    setMenuMutation (state, newMenu) {
      setMenu(newMenu)
      state.menu = newMenu
    }
  },
  actions: {
    async getMenuAction (txt) {
      const res = await getMenuList()
      // console.log(res.data)
      txt.commit('setMenuMutation', res.data)
    }
  }
}

export default user
